我无法弄清楚为什么我的按钮的onClick方法无效。每次我点击按钮,我的logcat都会显示ViewPostImeInputStage Action_Down。
我感谢任何帮助!
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
BluetoothConnect fragment = new BluetoothConnect();
transaction.add(R.id.sample_content_fragment, fragment);
transaction.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
这是我的片段。
公共类BluetoothConnect扩展了Fragment {
//For debugging purposes
private static final String TAG = "BluetoothConnect";
// Intent request codes
private static final int REQUEST_CONNECT_DEVICE_SECURE = 1;
private static final int REQUEST_CONNECT_DEVICE_INSECURE = 2;
private static final int REQUEST_ENABLE_BT = 3;
//Main buttons
private Button shareButton;
private Button listenButton;
private TextView test;
/**
* Name of the connected device
*/
private String mConnectedDeviceName = null;
/**
* Local Bluetooth adapter
*/
private BluetoothAdapter mBluetoothAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
//Get local Bluetooth adapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//If the adapter is null, then Bluetooth is not supported
if (mBluetoothAdapter == null) {
FragmentActivity activity = getActivity();
Toast.makeText(activity, "Bluetooth is not available", Toast.LENGTH_LONG).show();
activity.finish();
}
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_main, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
listenButton = (Button) view.findViewById(R.id.listen);
shareButton = (Button) view.findViewById(R.id.share);
test = (TextView) view.findViewById(R.id.test);
}
@Override
public void onStart() {
super.onStart();
//If BT is not on, request that it be enabled
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
} else {
setupButtons();
}
}
@Override
public void onDestroy() {
super.onDestroy();
//Figure out what to do if we need to destroy
}
@Override public void onResume() {
super.onResume();
//figure out what to do if person pauses out of app
}
public void setupButtons() {
Log.d(TAG, "setupButtons()");
//Initialize the music buttons
listenButton.setOnClickListener(new ButtonOnClickListener("listen"));
Log.d(TAG, "finished listenButton");
shareButton.setOnClickListener(new ButtonOnClickListener("share"));
}
private class ButtonOnClickListener implements View.OnClickListener {
String type;
public ButtonOnClickListener(String button) {
Log.d(TAG, this.type);
this.type = button;
}
public void onClick(View view) {
if (this.type == "listen") {
listenMusic();
} else {
shareMusic();
}
}
}
private void ensureDiscoverable() {
if (mBluetoothAdapter.getScanMode() !=
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
}
private void listenMusic() {
Log.d(TAG, "OMG ITS WORKING");
}
private void shareMusic() {
Log.d(TAG, "OMG ITS WORKING");
}
}
非常感谢!