您现在的位置是:首页 > 文章详情

关于Button点击弹出输入框,并且通过Servlet连接数据库

日期:2017-07-02点击:248
应主管的要求,做了一个月的 Android,对Android也发生了许多的问题,下面是主管让我弄的一个弹出输入框,能够输入评论,保存到 数据库的功能
这是MainActivity类中的内容
// 评论按钮
comment = (Button) findViewById(R.id.comment);
comment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// LayoutInflater inflater = getLayoutInflater();
// final View layout = inflater.inflate(R.layout.comment,(ViewGroup) findViewById(R.id.dialog));
// 获取文本
final EditText inputServer = new EditText(MainActivity.this);
// 设置获取文本内容为true
inputServer.setFocusable(true);
if(MainActivity.CityName == null){
inputServer.setText("你还未搜索当前城市,请定位后再评论");
}
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("评价框").setView(inputServer);
builder.setNegativeButton("取消", null);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface arg0, int arg1) {
String text = inputServer.getText().toString();
if(MainActivity.CityName == null){
HttpGet httpGet = new HttpGet("http://192.168.11.135:8081/MySql/pinlunServlet?cityName=null&comment=" + text);
try {
HttpResponse response = new DefaultHttpClient().execute(httpGet);
if(response.getStatusLine().getStatusCode() < 2000){
Log.i("add", "评论成功");
}else{
Log.e("Err", "评论失败");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else{
HttpGet httpGet = new HttpGet("http://192.168.11.135:8081/MySql/pinlunServlet?cityName=" + MainActivity.CityName + "&comment=" + text);
try {
HttpResponse response = new DefaultHttpClient().execute(httpGet);
if(response.getStatusLine().getStatusCode() < 2000){
Log.i("add", "评论成功");
}else{
Log.e("Err", "评论失败");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.i("add",text);
}});
builder.show();
}
});

这是comment.xml中的配置
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#ffffffff" android:orientation="horizontal"
android:id="@+id/dialog">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/tvname"
android:text="评论:" />
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/etname"
android:minWidth="100dip"/>
</LinearLayout>
AndroidManifest.xml中权限(仅供参考,如有缺少可以添加)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.baudumap"
android:versionCode="1"
android:versionName="1.0" >
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" >
</uses-sdk>
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.baudumap" >
</instrumentation>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_LOGS" >
</uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/wing"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="android.test.runner" />
<activity
android:name="com.example.baudumap.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>
</application>
</manifest>


最新内容请见作者的GitHub页:http://qaseven.github.io/

原文链接:https://yq.aliyun.com/articles/134720
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章